package com.ma.dp.a01;

import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName Solution70
 * @Author: mayongqiang
 * @DATE 2022/4/7 9:48
 * @Description: 编辑距离
 */
public class Solution70 {
    /*
    给你两个单词word1 和word2， 请返回将word1转换成word2 所使用的最少操作数 。
        你可以对一个单词进行如下三种操作：     插入一个字符、删除一个字符、替换一个字符
    1、dp[i][j]的定义： word1[1..i] word2[1..j]的最小编辑距离
    2、base case: dp[0][j]表示字符串1为空，最小编辑距离为删除字符串2的所有字符  dp[0][j] = j   dp[i][0] = i
    3、状态转移:
        if(word1[i]==word2[j]): 相同的字符则不进行任何操作
                dp[i][j] = dp[i-1][j-1];
        else:  遇到不同  有三种选择   插入/删除/替换
                1）、dp[i][j-1]+1    在s1[i]处插入与s2[j]相同的元素，j前移
                2）、dp[i-1][j]+1    删除s1[i]处的元素，i前移
                3）、dp[i-1][j-1]+1  将s1[i]替换为s2[j]

     */
    public static void main(String[] args) {
        /*输入：word1 = "horse", word2 = "ros" 输出：3*/
        //[2 0 2 0 3]
        System.out.println("\t"+minDistanceDetail("intention", "execution").val);
        //3320310000
        //word1 = "intention", word2 = "execution"
        //         execution
    }

    public static int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        // base case
        for (int i = 1; i <= m; i++) {
            dp[i][0] = i;
        }
        for (int j = 1; j <= n; j++) {
            dp[0][j] = j;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = min(
                            dp[i - 1][j] + 1,
                            dp[i][j - 1] + 1,
                            dp[i - 1][j - 1] + 1
                    );
                }
            }
        }
        return dp[m][n];
    }

    static int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    static int minChoice(Node a, Node b, Node c) {
        if (min(a.val, b.val, c.val) == a.val) {
            return 2;
        } else if (min(a.val, b.val, c.val) == b.val) {
            return 1;
        }else{
            return 3;
        }
    }

    public static Node minDistanceDetail(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        Node[][] dp = new Node[m + 1][n + 1];
        for (int i = 0; i < m + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                dp[i][j] = new Node();
            }
        }
        // base case
        for (int i = 1; i <= m; i++) {
            dp[i][0].val = i;
            dp[i][0].choice = 2;
        }
        for (int j = 1; j <= n; j++) {
            dp[0][j].val = j;
            dp[0][j].choice = 1;
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j].val = dp[i - 1][j - 1].val;
                    dp[i][j].choice = 0;
                } else {
                    dp[i][j].val = min(
                            dp[i - 1][j].val + 1,
                            dp[i][j - 1].val + 1,
                            dp[i - 1][j - 1].val + 1
                    );
                    dp[i][j].choice = minChoice(
                            dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]
                    );
                }
            }
        }
        List<Integer> res = new ArrayList<>();
        int m1 = m, n1 = n;
        while (m1 != 0 && n1 != 0) {
            res.add(dp[m1][n1].choice);
            switch (dp[m1][n1].choice) {
                case 0:
                case 3:
                    m1--;
                    n1--;
                    break;
                case 1:
                    n1--;
                    break;
                case 2:
                    m1--;
                    break;
            }
        }
        Collections.reverse(res);
        for(int choice: res){
            System.out.print(choice);
        }
        return dp[m][n];
    }

    //改造一下，记录下来最小编辑距离的编辑操作
    static class Node {
        int val ;
        int choice ;

        public Node() {

        }
        //0不变 1插入 2删除  3替换
    }
}
